package com.rui.study.commons.util;

import com.rui.study.commons.model.BizException;
import com.rui.study.commons.model.StatusCode;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @program: study_20180903
 * @description: BASE64加密算法工具类
 * @author: Yaowr
 * @create: 2018-09-29 13:54
 **/
public class Base64Util {


    public static String encode(byte[] bytes) {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }

    public static byte[] decode(String s) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            return decoder.decodeBuffer(s);
        } catch (IOException e) {
            throw new BizException(StatusCode.SYSTEM_EXCEPTION);
        }
    }

    private static void test1() throws UnsupportedEncodingException {
        String str = encode("abc".getBytes("iso8859-1"));
        System.out.println(str);

        String result = new String(decode(str), "iso8859-1");
        System.out.println(result);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        test1();
    }

}
